package coml.dzsw.base.proxy.statik;


import coml.dzsw.base.proxy.ISubject;

public class SubjectStaticProxy implements ISubject {

    private Class<? extends ISubject> target;

    private ISubject subject;

    public SubjectStaticProxy(Class<? extends ISubject> target) {
        this.target = target;
    }

    @Override
    public void doSomething(String param) {
        // change param
        String p = "this is " + param + ".";
        System.out.println("before do business.");
        this.getSubject().doSomething(p);
        System.out.println("after do business.");
    }

    public synchronized ISubject getSubject() {
        if (this.subject == null) {
            try {
                this.subject = this.target.getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return this.subject;
    }

}
